const path = require('path')
const isProd = process.env.NODE_ENV === 'production'
let externals = {}
const cdn = {
  css: ['https://unpkg.com/element-ui/lib/theme-chalk/index.css'
  ],
  js: []
}
if (isProd) {
  externals = {
    vue: 'Vue',
    echarts: 'echarts',
    'element-ui': 'ELEMENT',
    xlsx: 'XLSX',
    'cos-js-sdk-v5': 'COS',
    video: 'Video'
  }
  cdn.js = [
    'https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js',
    'https://cdn.jsdelivr.net/npm/echarts@5.2.1/dist/echarts.min.js',
    'https://unpkg.com/element-ui/lib/index.js',
    'https://cdn.jsdelivr.net/npm/xlsx@0.16.6/dist/xlsx.full.min.js',
    'https://unpkg.com/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js',
    'https://cdn.bootcdn.net/ajax/libs/video.js/7.15.0/alt/video.core.js'
  ]
}
module.exports = {
  lintOnSave: false,
  publicPath: './',
  outputDir: 'dist',
  assetsDir: 'static',
  configureWebpack: {
    externals
  },
  chainWebpack: config => {
    config
      .plugin('html')
      .tap(args => {
        args[0].cdn = cdn
        return args
      })
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .test(/.svg$/)
      .include.add(path.resolve(__dirname, './src/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
    const fileRule = config.module.rule('file')
    fileRule.uses.clear()
    fileRule
      .test(/.svg$/)
      .exclude.add(path.resolve(__dirname, './src/icons/svg'))
      .end()
      .use('file-loader')
      .loader('file-loader')
  }
}
